package cn.onein.edu.manage.util;
/**
 * Title:        ParamUtil
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:      GTM
 * @author:      GTM
 * @version      1.0
 */
import javax.servlet.http.*;

public class ParamUtil{

    public static String[] getStringArray(HttpServletRequest request, String paramName) {
   /*try {
      request.setCharacterEncoding("gb2312");
      String a=request.getCharacterEncoding();
      int k=0;
    }
    catch (java.io.UnsupportedEncodingException uee) {
      uee.printStackTrace(System.out);
    }*/
    String[] temp = request.getParameterValues(paramName);
    try
    {
        for(int i=0;i<temp.length;i++)
            temp[i] = new String(temp[i].getBytes("ISO-8859-1"), "gbk");
    }
    catch(Exception e)
    {

    }
      return temp;
  }

  public static String getString(HttpServletRequest request, String paramName) {
   /*try {
      request.setCharacterEncoding("gb2312");
      String a=request.getCharacterEncoding();
      int k=0;
    }
    catch (java.io.UnsupportedEncodingException uee) {
      uee.printStackTrace(System.out);
    }*/
    String temp = request.getParameter(paramName);
    try
    {
        //temp = new String(temp.getBytes("ISO-8859-1"), "gbk");
    }
    catch(Exception e)
    {

    }
    if (temp != null && !temp.equals(""))
      return temp;
    else
      return "";
  }

  public static String getString(HttpServletRequest request, String paramName,
                                 String defaultString) {
    String temp = getString(request, paramName);
    if (temp != null && !temp.equals(""))
      return temp;
    else
      return defaultString;
  }

  public static int getInt(HttpServletRequest request, String paramName) throws
      NumberFormatException {
    String  tmpstr=getString(request, paramName,"");
    tmpstr=tmpstr.equals("")?"0":tmpstr;
    return Integer.parseInt(tmpstr);
  }

  public static int getInt(HttpServletRequest request, String paramName,
                           int defaultInt) {
    try {
      String temp = getString(request, paramName);
      if (temp == null || temp.equals("") || !isNumber(temp))
        return defaultInt;
      else
        return Integer.parseInt(temp);
    }
    catch (NumberFormatException e) {
      e.printStackTrace();
      return 0;
    }
  }

  public static float getFloat(HttpServletRequest request, String paramName) throws
      NumberFormatException {
    return Float.parseFloat(getString(request, paramName));
  }

  public static float getFloat(HttpServletRequest request, String paramName,
                               float defaultfloat) {
    try {
      String temp = getString(request, paramName);
      if (temp == null || temp.equals(""))
        return defaultfloat;
      else
        return Float.parseFloat(temp);
    }
    catch (NumberFormatException e) {
      e.printStackTrace();
      return 0;
    }
  }

  public static boolean isNumber(String pstr) {
    String strRef = "1234567890";
    if ("".equals(pstr))
      return false;
    for (int i = 0; i < pstr.length(); i++) {
      String tempChar = pstr.substring(i, i + 1);
      if (strRef.indexOf(tempChar, 0) == -1) {
        return false;
      }
    }
    return true;
  }

  public static int instrNum(String sstr,String objstr) {
  //  int num=0;
 //   int j=0;
 //     while(sstr.indexOf(objstr,j) >=0) {
//
//          num+=1;
//          j=sstr.indexOf(objstr,j)+1;
//      }
    	String[] thestr=sstr.split(objstr);
	  return thestr.length;
  }
  public static boolean getBoolean(HttpServletRequest request,
                                   String paramName) {
      String tmpstr = getString(request, paramName, "");
      boolean v;
      if (tmpstr.equals("1") || tmpstr.equals("是") || tmpstr.equals("yes") ||
          tmpstr.equals("Yes") || tmpstr.equals("true") ||
          tmpstr.equals("True")) {
          v = true;
      }
      else {
          v = false;
      }
      return v;
  }

  public static boolean getBoolean(HttpServletRequest request,
                                   String paramName,
                                   String defaultBoolean) {
      String tmpstr = getString(request, paramName, defaultBoolean);
      boolean v;
      if (tmpstr.equals("1") || tmpstr.equals("是") || tmpstr.equals("yes") ||
          tmpstr.equals("Yes") || tmpstr.equals("true") ||
          tmpstr.equals("True")) {
          v = true;
      }
      else {
          v = false;
      }
      return v;
  }
}
